/**
 * 
 */
package br.com.skynix.tcmc.base.managedbeans;

import java.util.List;
import java.util.Map;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import br.com.skynix.tcmc.base.model.EntidadeAuditavel;
import br.com.skynix.tcmc.base.spring.IServiceAuditavel;

/**
 * @author mgrb
 * 
 */
public abstract class AbstractDataProvider<T extends EntidadeAuditavel> extends
		LazyDataModel<T> {

	protected abstract IServiceAuditavel<T> getService();

	protected abstract T getModelo();

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.primefaces.model.LazyDataModel#load(int, int, java.lang.String,
	 * org.primefaces.model.SortOrder, java.util.Map)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public List<T> load(int first, int pageSize, String sortField,
			SortOrder sortOrder, Map filters) {
		List<T> listaEntidade = getService().buscarPeloExemplo(getModelo(),
				first, pageSize, sortField, sortOrder);
		return listaEntidade;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.primefaces.model.LazyDataModel#getRowData(java.lang.String)
	 */
	@Override
	public T getRowData(String rowKey) {
		return rowKey == null || "null".equals(rowKey) ? null : getService()
				.buscarPelaChave(Long.valueOf(rowKey));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.primefaces.model.LazyDataModel#getRowKey(java.lang.Object)
	 */
	@Override
	public Object getRowKey(T object) {
		return object.getId();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.primefaces.model.LazyDataModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		return getService().getQuantidadeRegistros(getModelo());
	}

}
